package com.dhemery.mack.adapters;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;

import com.dhemery.mack.model.Folder;
import com.dhemery.mack.model.Node;
import com.dhemery.mack.model.Page;

public class NodeAdapterFactory implements IAdapterFactory {
	@SuppressWarnings("unchecked")
	private final Class[] adapterClasses = new Class[] {
					IEditorInput.class,
					IWorkbenchAdapter.class,
					};
	
	private final FolderInformationProvider folderAdapter;
	private final NodeInformationProvider nodeAdapter;
	private final NodeImageDescriptorFactory imageDescriptorFactory;

	public NodeAdapterFactory(ImageDescriptorSource imageDescriptorSource) {
		imageDescriptorFactory = new NodeImageDescriptorFactory(imageDescriptorSource);
		folderAdapter = new FolderInformationProvider(imageDescriptorFactory);
		nodeAdapter = new NodeInformationProvider(imageDescriptorFactory);
	}

	@SuppressWarnings("unchecked")
	public Object getAdapter(Object object, Class adapterType) {
		if(adapterType == IEditorInput.class) return getEditorInputAdapter(object);
		if(adapterType == IWorkbenchAdapter.class) return getWorkbenchAdapter(object);
		return null;
	}

	private IEditorInput getEditorInputAdapter(Object object) {
		if(object instanceof Node) {
			ImageDescriptor imageDescriptor = imageDescriptorFactory.createImageDescriptor((Node)object);
			return new NodeEditorInput((Node)object, imageDescriptor);
		}
		return null;
	}

	private IWorkbenchAdapter getWorkbenchAdapter(Object object) {
		if(object instanceof Folder) return folderAdapter;
		if(object instanceof Page) return nodeAdapter;
		return null;
	}

	@SuppressWarnings("unchecked")
	public Class[] getAdapterList() {
		return adapterClasses;
	}
}
